package com.Server.model;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TEST  extends Thread {

	private static final Logger _log = Logger.getLogger(TEST.class.getName());
	private static boolean _inerror;

	/* buttons可以是以下的值
	 * java.awt.event.InputEvent.BUTTON1_MASK 代表左鍵
	 * java.awt.event.InputEvent.BUTTON2_MASK 代表中鍵
	 * java.awt.event.InputEvent.BUTTON3_MASK 代表右鍵*/
	private void runBat(){
		try{
			//		     File timebat=new File("C:/settime.bat");
			//		     timebat.createNewFile(); //建立新檔
			Process p=Runtime.getRuntime().exec("TEST.bat"); //執行DOS指令settime.bat
			p.waitFor();
			p.destroy(); //破壞物件
			// timebat.delete(); //刪除settime.bat檔

			Thread.sleep(1000);
//			Robot to = new Robot();
//			to.mouseMove(1866, 177);
//			System.out.println("滑鼠移動到 1866 , 177");
//			Thread.sleep(300);
//			to.mousePress(InputEvent.BUTTON1_MASK);
//			Thread.sleep(300);
//			System.out.println("滑鼠按下左鍵");
//			to.mouseRelease(InputEvent.BUTTON1_MASK);
//
//			Thread.sleep(3000);
			System.out.println("重新連線刷新完成。");
			_inerror = false;
		}catch(Exception e){
			_log.log(Level.SEVERE, e.getLocalizedMessage(),e);
		}
	}

	@Override
	public void run() {
		while(true){
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
				if(_inerror){
					return;
				}
				//			InetAddress address = InetAddress.getByName("et.eatuo.com");
				//			System.out.println(address.isReachable(5000));
				//		} catch (UnknownHostException e) {
				//			e.printStackTrace();
				//		} catch (IOException e) {
				//			e.printStackTrace();
				//		}
				try {
//		            InetAddress address = 
		            		InetAddress.getByName("www.google.com.tw");//www.google.com.tw
//		            System.out.println(address.isReachable(1000));
//		            System.out.print(address.getHostAddress());
		        } catch (UnknownHostException e) {
		        	runBat();
		        	System.out.println("網路異常。");
//		            e.printStackTrace();
		        }
//				try {
//					 URL myURL = new URL("http://tw.yahoo.com");
//					URLConnection myURLConnection = myURL.openConnection();
//					myURLConnection.connect();
//					myURLConnection.setReadTimeout(300);
//					System.out.println(myURL.getHost());
//				}
//				catch (MalformedURLException e) {
//					// new URL() failed
//					// ...
//					_inerror = true;
//					System.out.println("偵測到網路異常....執行重新連線。");
//					runBat();
//				} catch (IOException e) {
//					_inerror = true;
//					// openConnection() failed
//					// ...
//					System.out.println("偵測到網路異常....執行重新連線。");
//					runBat();
//				}
		}
	}


}